package com.gsbelarus.gedemin.income.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gsbelarus.gedemin.income.table.DrawerListTable;
import com.gsbelarus.gedemin.income.R;

import java.util.ArrayList;

public class DrawerListAdapter extends ArrayAdapter<DrawerListTable> {

    public static final int MAIN_ITEM = 0;
    public static final int OTHER_ITEM = 1;

    private int countMainItems;

    public DrawerListAdapter(Context context, int countMainItems) {
        super(context, R.layout.drawer_list_item_other, new ArrayList<DrawerListTable>());
        this.countMainItems = countMainItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DrawerListTable item = getItem(position);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            switch (getItemViewType(position)) {
                case MAIN_ITEM:
                    convertView = createMainItem(inflater, item, position);
                    break;
                case OTHER_ITEM:
                    convertView = createOtherItem(inflater, item, position);
                    break;
            }
        }
        return convertView;
    }

    private View createOtherItem(LayoutInflater inflater, DrawerListTable item, int position) {
        View convertView = inflater.inflate(R.layout.drawer_list_item_other, null);

        if (position == countMainItems) {
            /** показываем общий разделитель между основным меню и дополнительным*/
            View view = convertView.findViewById(R.id.drawer_list_divider_main_other);
            if (view != null)
                view.setVisibility(View.VISIBLE);
//            /** скрываем межэлементный разделитель*/
//            view = convertView.findViewById(R.id.drawer_list_divider);
//            if (view != null)
//                view.setVisibility(View.INVISIBLE);
        }

        ImageView image = (ImageView) convertView.findViewById(R.id.image_drawer_list_item_other);
        if (image != null)
            image.setImageDrawable(item.getImage());

        TextView textView = (TextView) convertView.findViewById(R.id.text_drawer_list_item_other);
        if (textView != null)
            textView.setText(item.getName());

        return convertView;
    }

    private View createMainItem(LayoutInflater inflater, DrawerListTable item, int position) {
        View convertView = inflater.inflate(R.layout.drawer_list_item_main, null);

//        if (position == 0) {
//            View view = convertView.findViewById(R.id.drawer_list_divider);
//            if (view != null)
//                view.setVisibility(View.INVISIBLE);
//        }

        TextView textView = (TextView) convertView.findViewById(R.id.text_drawer_list_item);
        if (textView != null)
            textView.setText(item.getName());
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return (getItem(position).getType() == MAIN_ITEM) ? MAIN_ITEM : OTHER_ITEM;
    }
}
